package com.sun.algorithm.linked;


import org.junit.Test;

/**
 * 单向链表
 */
public class SinglyLinkedList {
   @Test
   public void test01(){
      HeroNode h1 = new HeroNode(1, "宋江", "及时雨");
      HeroNode h2 = new HeroNode(2, "卢俊义", "玉麒麟");
      HeroNode h3 = new HeroNode(3, "吴用", "智多星");
      HeroNode h4 = new HeroNode(4, "武大郎", "喝药");

      singleLinkedList s1 = new singleLinkedList();
      s1.add(h1);
      s1.add(h2);
      s1.add(h3);
      s1.add(h4);

      s1.list();

      s1.update(new HeroNode(2,"老子","老子啊"));

      s1.delete(1);
      s1.delete(2);
      s1.delete(3);
      s1.delete(4);
   }

}
//定义singleLinkedList
class singleLinkedList{
   //初始化头节点,头节点不要动，
   private HeroNode head = new HeroNode(0,"","");
   //添加方法
   /**
    * 不考虑顺序
    * 找到当前链表的最后节点
    * 将最后这个节点只想新的节点
    * @param node
    */
   public void add(HeroNode node){
      //因为head节点不能动，因此我们需要一个辅助节点
      HeroNode temp = head;
      //遍历链表 找到最后
      while (temp.next != null){
         //如果不是最后一个 将后移一位
         temp = temp.next;
      }
      //当退出循环时，temp.next指向了链表的最后
      temp.next = node;
   }


   //遍历
   public void list(){
      //判断链表是否为空
      if (head.next == null){
         System.out.println("链表为空(~_~)");
         return;
      }
      //头节点不能动
      HeroNode temp = head.next;
      while (temp != null){
         System.out.println(temp);
         //将指针后移
         temp = temp.next;
      }
      System.out.println("遍历结束了(~@@@@~)");
   }

   //修改节点
   public void update(HeroNode newHeroNode){
      if (head.next == null){
         System.out.println("链表为空~~");
         return;
      }

      HeroNode temp = head.next;
      boolean flag = false;
      while (true){
         if (temp == null){
            break;
         }
         if (temp.no == newHeroNode.no){
            //找到了
            flag = true;
            break;
         }
         temp = temp.next;
      }
      if (flag){
         temp.name = newHeroNode.name;
         temp.nickName = newHeroNode.nickName;
      }else {
         System.out.println("没有找到对应数据（~~）");
      }
      list();
   }

   public void delete(int no){
      if (head.next == null){
         System.out.println("链表是空的啊！！");
         return;
      }
      HeroNode temp = head;
      boolean flag = false;
      while (true){
         if (temp.next == null){
            break;
         }
         if (temp.next.no == no){
            flag = true;
            break;
         }
         temp = temp.next;
      }

      if (flag){
         //找到了
         temp.next = temp.next.next;
      }else {
         System.out.println("链表未找到no="+no);
      }
      list();
   }
}


class HeroNode{
   public int no;
   public String name;
   public String nickName;
   public HeroNode next;

   public HeroNode() {
   }

   public HeroNode(int no, String name, String nickName) {
      this.no = no;
      this.name = name;
      this.nickName = nickName;
   }

   public HeroNode(int no, String name, String nickName, HeroNode next) {
      this.no = no;
      this.name = name;
      this.nickName = nickName;
      this.next = next;
   }

   @Override
   public String toString() {
      return "HeroNode{" +
              "no=" + no +
              ", name='" + name + '\'' +
              ", nickName='" + nickName + '}';
   }
}



